package com.ui.basic.service.masterdata;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.Globals;
import com.ui.basic.assembler.masterdata.IdentificationAssembler;
import com.ui.basic.command.masterdata.CommandIdentification;
import com.ui.basic.ibatis.dao.masterdata.TblMIdentificationDAO;
import com.ui.basic.ibatis.model.masterdata.TblMIdentification;
import com.ui.basic.ibatis.model.masterdata.TblMIdentificationExample;

@Service("identificationService")
public class IdentificationServiceImpl implements IdentificationService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(IdentificationServiceImpl.class);
	
	@Autowired
	private TblMIdentificationDAO tblMIdentificationDAO;

	public void setTblMIdentificationDAO(TblMIdentificationDAO tblMIdentificationDAO) {
		this.tblMIdentificationDAO = tblMIdentificationDAO;
	}

	@Transactional(readOnly = true)
	public CommandIdentification getIdentification(
			CommandIdentification commandIdentification) throws SQLException {
		LOGGER.debug(">>>IdentificationServiceImpl.getIdentification");
		if (commandIdentification != null) {
			TblMIdentificationExample example = new TblMIdentificationExample();

			int rec = 0;
			try {
				rec = tblMIdentificationDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandIdentification.calculate(rec);
			example.setIntRowNum((commandIdentification.getPgIndex() - 1)
					* commandIdentification.getPgRows());
			example.setIntRowsPerPage(commandIdentification.getPgRows());
			example.setOrderByClause("CODE");
			commandIdentification.setData(tblMIdentificationDAO.selectByExample(example));
		}
		return commandIdentification;
	}

	@Transactional(readOnly = true)
	public CommandIdentification getIdentificationDetail(
			CommandIdentification commandIdentification) throws SQLException {
		LOGGER.debug(">>>IdentificationServiceImpl.getIdentificationDetail");
		commandIdentification.setProperty(tblMIdentificationDAO.selectByPrimaryKey(commandIdentification.getId()));
		return commandIdentification;
	}

	@Transactional
	public void save(CommandIdentification commandIdentification)
			throws SQLException {
		LOGGER.debug(">>>IdentificationServiceImpl.save");
		IdentificationAssembler assembler = new IdentificationAssembler();
		TblMIdentification identification = assembler.toEntity(commandIdentification);

		if(commandIdentification.getId() != null){
			identification.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			identification.setChangeddate(new Date());
			try {
				tblMIdentificationDAO.updateByPrimaryKeySelective(identification);

			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}else{
			identification.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			identification.setCreateddate(new Date());
			try {
				tblMIdentificationDAO.insert(identification);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

	public void delete(CommandIdentification commandIdentification)
			throws SQLException {
		LOGGER.debug(">>>IdentificationServiceImpl.save");
		TblMIdentificationExample example = new TblMIdentificationExample();
		
		if(commandIdentification.getId()!=null){
			example.createCriteria().andIdEqualTo(commandIdentification.getId());
		}else if(commandIdentification.getSelectedRecord()!=null && commandIdentification.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandIdentification.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblMIdentificationDAO.deleteByExample(example);				
	}
	
	
}
